/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.service.tmessage;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;
import com.boxin.ims.modules.wechat.entity.tmessage.UserMessageTemplate;
import com.boxin.ims.modules.wechat.dao.tmessage.UserMessageTemplateDao;

/**
 * 用户消息模板Service
 * @author 肖乾
 * @version 2014-09-11
 */
@Component
@Transactional(readOnly = true)
public class UserMessageTemplateService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(UserMessageTemplateService.class);
	
	@Autowired
	private UserMessageTemplateDao userMessageTemplateDao;
	
	public UserMessageTemplate get(Long id) {
		return userMessageTemplateDao.findOne(id);
	}
	
	public Page<UserMessageTemplate> find(Page<UserMessageTemplate> page, UserMessageTemplate userMessageTemplate) {
		DetachedCriteria dc = userMessageTemplateDao.createDetachedCriteria();
		if (userMessageTemplate.getUser()!=null && userMessageTemplate.getUser().getId()>0){
			dc.add(Restrictions.eq("user.id", userMessageTemplate.getUser().getId()));
		}
		if (StringUtils.isNotEmpty(userMessageTemplate.getName())){
			dc.add(Restrictions.like("name", "%"+userMessageTemplate.getName()+"%"));
		}
		dc.addOrder(Order.desc("id"));
		return userMessageTemplateDao.find(page, dc);
	}
	
	public UserMessageTemplate find(UserMessageTemplate userMessageTemplate) {
		DetachedCriteria dc = userMessageTemplateDao.createDetachedCriteria();
		if (userMessageTemplate.getUser()!=null && userMessageTemplate.getUser().getId()>0){
			dc.add(Restrictions.eq("user.id", userMessageTemplate.getUser().getId()));
		}
		if (userMessageTemplate.getBusinessMessageTemplate()!=null &&StringUtils.isNotEmpty(userMessageTemplate.getBusinessMessageTemplate().getName())){
			dc.add(Restrictions.eq("businessMessageTemplate.name", userMessageTemplate.getBusinessMessageTemplate().getName()));
		}
		List<UserMessageTemplate> list= userMessageTemplateDao.find( dc);
		if(list.size()==1){
			return list.get(0);
		}else{
			return null;
		}
	}
	
	
	@Transactional(readOnly = false)
	public Long getUserMessageTemplateCountByBid(UserMessageTemplate userMessageTemplate) {
		DetachedCriteria dc = userMessageTemplateDao.createDetachedCriteria();
		if (userMessageTemplate.getUser()!=null && userMessageTemplate.getUser().getId()>0){
			dc.add(Restrictions.eq("user.id", userMessageTemplate.getUser().getId()));
		}
		if (userMessageTemplate.getBusinessMessageTemplate()!=null && userMessageTemplate.getBusinessMessageTemplate().getId()>0){
			dc.add(Restrictions.eq("businessMessageTemplate.id", userMessageTemplate.getBusinessMessageTemplate().getId()));
		}
		return  userMessageTemplateDao.count(dc);
	}
	
	
	@Transactional(readOnly = false)
	public void save(UserMessageTemplate userMessageTemplate) {
		userMessageTemplateDao.save(userMessageTemplate);
	}
	
	@Transactional(readOnly = false)
	public UserMessageTemplate getUserMessageTemplateByBid(Long userId,String bCode) {
		return userMessageTemplateDao.getUserMessageTemplateByBid(userId, bCode);
	}
	
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		userMessageTemplateDao.deleteById(id);
	}
	
}
